//
//  JMSettingsController.m
//  NewiOSProject
//
//  Created by li on 2023/9/13.
//  Copyright © 2023 范智渊. All rights reserved.
//

#import "JMSettingsController.h"
#import "LayoutView.h"

#import "JMPickerSelectionView.h"
#import "JMPickerSelectionModel.h"
#import "PaddingView.h"
#import "LayoutScrollView.h"
#import "CustomStackView.h"
#import "CustomStackView.h"
#import "LayoutView.h"
#import "UIView+PopupProtocol.h"
#import "JMAuthentiactionConfirmView.h"

#import "JMMyFeedbackController.h"
#import "JMLoginVC.h"
@interface JMSettingsController ()
@property(nonatomic, strong) UIStackView *rootStack;
@property(nonatomic, strong) LayoutScrollView *scrollView;
@property(nonatomic, strong) JMAuthentiactionConfirmView *confirmView;
@end

@implementation JMSettingsController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.TitleLabel.text = @"Configurar";
    self.view.backgroundColor = WhiteColor();
    
    [self setupViews];
    
    
}

- (void)setupViews {
    
    [self.view addSubview:self.rootStack];
    [self.rootStack addArrangedSubview:self.scrollView];
    self.scrollView.rootStack.contentView.spacing = 0;
    
    for(NSInteger i = 0; i < 2; i ++) {
        CustomStackView *h_stack = [CustomStackView init:(UILayoutConstraintAxisHorizontal)];
        h_stack.alignment = UIStackViewAlignmentCenter;
        
        UILabel *title = [UILabel new];
        title.font = [UIFont systemFontOfSize:14];
        title.textColor = [UIColor colorWithHexString:@"#0F1511"];
        
        [h_stack addArrangedSubview:title];
        
        CustomStackView *h_stack_r = [CustomStackView init:(UILayoutConstraintAxisHorizontal)];
        h_stack_r.alignment = UIStackViewAlignmentCenter;
        h_stack_r.spacing = 20;
        
        UIImageView *more = [UIImageView new];
        [more.widthAnchor constraintEqualToConstant:7].active = YES;
        more.image = [UIImage imageNamed:@"more_right"];
        
        if (i == 0) {
            [h_stack_r addArrangedSubview:more];
            title.text = @"Realimentación";
        }else if (i == 1) {
            title.text = @"Número de versión";
            UILabel *version = [UILabel new];
            version.font = [UIFont systemFontOfSize:14];
            version.textColor = [UIColor colorWithHexString:@"#9B9B9B"];
            NSString *versionV =  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
            version.text = [NSString stringWithFormat:@"Versi %@", versionV];
            [h_stack_r addArrangedSubview:version];
        }
        
        [h_stack addArrangedSubview:h_stack_r];
        
        [self.scrollView.rootStack addContentView: [[PaddingView initWithContentView:h_stack edgeInsets:(UIEdgeInsetsMake(26, 16, 26, 16))] then:^(PaddingView * _Nonnull view) {
            [view.widthAnchor constraintEqualToConstant: kScreenWidth - 32].active = YES;
            view.backgroundColor = [UIColor whiteColor];
            view.layer.cornerRadius = 20;
            view.layer.masksToBounds = YES;
        }]];
        CustomStackView *line = [CustomStackView init:(UILayoutConstraintAxisHorizontal)];
        [self.scrollView.rootStack addContentView: [[PaddingView initWithContentView:line edgeInsets:(UIEdgeInsetsMake(26, 16, 26, 16))] then:^(PaddingView * _Nonnull view) {
            [view.widthAnchor constraintEqualToConstant: kScreenWidth - 48].active = YES;
            view.backgroundColor = mainSepartorColor();
            [view.heightAnchor constraintEqualToConstant:1].active = YES;
        }]];
        
        if (i == 0) {
            [h_stack.superview addTapGestuer:^(UIView * _Nonnull view) {
                JMMyFeedbackController *c  = [JMMyFeedbackController new];
                [self.navigationController pushViewController:c animated:YES];
            }];
        }
    }
    
    [self.rootStack addArrangedSubview: [PaddingView initWithContentView:self.confirmView edgeInsets:(UIEdgeInsetsMake(20, 0, 64, 0))]];
}
- (void)logingout:(UIButton *)sender {
    [JMProjectManager.sharedJMProjectManager.loginUser clear];
    UINavigationController *firstNVC    = [[UINavigationController alloc]
                                        initWithRootViewController:[NSClassFromString(@"JMLoginVC") new]];
    [UIApplication sharedApplication].keyWindow.rootViewController = firstNVC;
    [[UIApplication sharedApplication].keyWindow makeKeyAndVisible];
    
}


#pragma mark - 懒加载
- (UIStackView *)rootStack {
    if (!_rootStack) {
        self.rootStack = [UIStackView new];
        _rootStack.alignment = UIStackViewAlignmentCenter;
        _rootStack.axis = UILayoutConstraintAxisVertical;
        _rootStack.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:_rootStack];
        [self.rootStack mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(@(NAVIGATION_BAR_HEIGHT()));
        }];
    }
    return  _rootStack;
}
- (LayoutScrollView *)scrollView {
    if (!_scrollView) {
        self.scrollView = [LayoutScrollView new];
        _scrollView.rootStack.contentView.axis = UILayoutConstraintAxisVertical;
        [_scrollView.widthAnchor constraintEqualToConstant: kScreenWidth].active = YES;
        _scrollView.contentInset = UIEdgeInsetsMake(30, 0, 0, 0);
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.rootStack.layoutViewAlignment = LayoutViewAlignmentTop;
    }
    return _scrollView;
}

- (JMAuthentiactionConfirmView *)confirmView {
    if (!_confirmView) {
        self.confirmView = [JMAuthentiactionConfirmView new];
        _confirmView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
         
        [_confirmView.widthAnchor constraintEqualToConstant:kScreenWidth - 32].active = YES;
        [_confirmView.confirmBut setTitle:@"Cerrar sesión" forState:(UIControlStateNormal)];
        [_confirmView.confirmBut addTarget:self action:@selector(logingout:) forControlEvents:(UIControlEventTouchUpInside)];
        _confirmView.confirmBut.enable = YES;
    }
    return _confirmView;
}


@end
